import android.content.Context
import android.view.LayoutInflater

import android.view.View
import android.widget.ArrayAdapter

import android.view.ViewGroup
import android.widget.BaseAdapter
import android.widget.ImageView
import android.widget.TextView
import androidx.recyclerview.widget.RecyclerView
import com.bumptech.glide.Glide
import com.example.gamecenter.R
import com.example.gamecenter.adapter.HomeRecommendCardItem
import com.example.gamecenter.adapter.ListHomeRecommendCardAdapter
import com.example.gamecenter.adapter.RecommendItem
import com.example.nanke.adapter.ListRecommendAdapter2


class ListRecommendAdapter(private val context: Context, data: List<RecommendItem>? = null) : BaseAdapter() {
    private val datas: MutableList<RecommendItem> =  data?.toMutableList() ?: mutableListOf()

    interface OnRecommendItemClickListener {
        fun toDetail(v: View?, parmas: RecommendItem?=null)
    }
    private var onRecommendItemClickListener: OnRecommendItemClickListener?= null;

    fun setOnRecommendItemClickListener(listener: OnRecommendItemClickListener) {
        onRecommendItemClickListener = listener
    }
    fun setDatas(d: List<RecommendItem>){
        datas.clear();
        datas.addAll(d)
        notifyDataSetChanged();
    }
    override fun getCount(): Int {
        return datas.size
    }

    override fun getItem(position: Int): Any {
        return datas[position]
    }

    override fun getItemId(position: Int): Long {
        return position.toLong()
    }

    override fun getView(position: Int, convertView: View?, parent: ViewGroup?): View {
        val view: View = convertView ?: LayoutInflater.from(context).inflate(R.layout.recommend_item, parent, false)
        val textView: TextView = view.findViewById(R.id.recommend_item_title)
        val imageView: ImageView = view.findViewById(R.id.recommend_item_image)
        val viewDetail: TextView = view.findViewById(R.id.recommend_item_view)
        val item: RecommendItem = datas[position]
        textView.text = item.title;
        Glide.with(context!!).load(item.imageUrl).into(imageView);

        viewDetail.setOnClickListener(object :View.OnClickListener{
            override fun onClick(p0: View?) {
                onRecommendItemClickListener?.toDetail(p0, item)
            }
        })

        return view
    }
}
